home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / USIN6P (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  5.1 KB  |  174 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.Icon;
  4. import com.sun.java.swing.JComponent;
  5. import com.sun.java.swing.JLabel;
  6. import com.sun.java.swing.JTree;
  7. import com.sun.java.swing.UIManager;
  8. import com.sun.java.swing.tree.TreeCellRenderer;
  9. import java.awt.Color;
  10. import java.awt.Component;
  11. import java.awt.Dimension;
  12. import java.awt.Graphics;
  13.  
  14. public class BasicTreeCellRenderer extends JLabel implements TreeCellRenderer {
  15.    protected boolean selected;
  16.    protected transient Icon closedIcon;
  17.    protected transient Icon leafIcon;
  18.    protected transient Icon openIcon;
  19.    protected Color textSelectionColor;
  20.    protected Color textNonSelectionColor;
  21.    protected Color backgroundSelectionColor;
  22.    protected Color backgroundNonSelectionColor;
  23.    protected Color borderSelectionColor;
  24.  
  25.    public BasicTreeCellRenderer() {
  26.       ((JLabel)this).setHorizontalAlignment(2);
  27.       this.setLeafIcon(UIManager.getIcon("Tree.leafIcon"));
  28.       this.setClosedIcon(UIManager.getIcon("Tree.closedIcon"));
  29.       this.setOpenIcon(UIManager.getIcon("Tree.openIcon"));
  30.       this.setTextSelectionColor(UIManager.getColor("Tree.textSelectionColor"));
  31.       this.setTextNonSelectionColor(UIManager.getColor("Tree.textNonSelectionColor"));
  32.       this.setBackgroundSelectionColor(UIManager.getColor("Tree.backgroundSelectionColor"));
  33.       this.setBackgroundNonSelectionColor(UIManager.getColor("Tree.backgroundNonSelectionColor"));
  34.       this.setBorderSelectionColor(UIManager.getColor("Tree.borderSelectionColor"));
  35.    }
  36.  
  37.    public Color getBackgroundNonSelectionColor() {
  38.       return this.backgroundNonSelectionColor;
  39.    }
  40.  
  41.    public Color getBackgroundSelectionColor() {
  42.       return this.backgroundSelectionColor;
  43.    }
  44.  
  45.    public Color getBorderSelectionColor() {
  46.       return this.borderSelectionColor;
  47.    }
  48.  
  49.    public Icon getClosedIcon() {
  50.       return this.closedIcon;
  51.    }
  52.  
  53.    public Icon getDefaultClosedIcon() {
  54.       return this.closedIcon;
  55.    }
  56.  
  57.    public Icon getDefaultLeafIcon() {
  58.       return this.leafIcon;
  59.    }
  60.  
  61.    public Icon getDefaultOpenIcon() {
  62.       return this.openIcon;
  63.    }
  64.  
  65.    public Icon getLeafIcon() {
  66.       return this.leafIcon;
  67.    }
  68.  
  69.    public Icon getOpenIcon() {
  70.       return this.openIcon;
  71.    }
  72.  
  73.    public Dimension getPreferredSize() {
  74.       Dimension retDimension = super.getPreferredSize();
  75.       if (retDimension != null) {
  76.          retDimension = new Dimension(retDimension.width + 3, retDimension.height);
  77.       }
  78.  
  79.       return retDimension;
  80.    }
  81.  
  82.    public Color getTextNonSelectionColor() {
  83.       return this.textNonSelectionColor;
  84.    }
  85.  
  86.    public Color getTextSelectionColor() {
  87.       return this.textSelectionColor;
  88.    }
  89.  
  90.    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
  91.       String stringValue = tree.convertValueToText(value, sel, expanded, leaf, row, hasFocus);
  92.       ((JLabel)this).setFont(((Component)tree).getFont());
  93.       ((JLabel)this).setText(stringValue);
  94.       if (sel) {
  95.          ((Component)this).setForeground(this.getTextSelectionColor());
  96.       } else {
  97.          ((Component)this).setForeground(this.getTextNonSelectionColor());
  98.       }
  99.  
  100.       if (leaf) {
  101.          ((JLabel)this).setIcon(this.getLeafIcon());
  102.       } else if (expanded) {
  103.          ((JLabel)this).setIcon(this.getOpenIcon());
  104.       } else {
  105.          ((JLabel)this).setIcon(this.getClosedIcon());
  106.       }
  107.  
  108.       this.selected = sel;
  109.       return this;
  110.    }
  111.  
  112.    public void paint(Graphics g) {
  113.       Color bColor;
  114.       if (this.selected) {
  115.          bColor = this.getBackgroundSelectionColor();
  116.       } else {
  117.          bColor = this.getBackgroundNonSelectionColor();
  118.          if (bColor == null) {
  119.             bColor = ((Component)this).getBackground();
  120.          }
  121.       }
  122.  
  123.       if (bColor != null) {
  124.          Icon currentI = ((JLabel)this).getIcon();
  125.          g.setColor(bColor);
  126.          if (currentI != null && ((JLabel)this).getText() != null) {
  127.             int offset = currentI.getIconWidth() + ((JLabel)this).getIconTextGap();
  128.             g.fillRect(offset, 0, ((JComponent)this).getWidth() - 1 - offset, ((JComponent)this).getHeight() - 1);
  129.          } else {
  130.             g.fillRect(0, 0, ((JComponent)this).getWidth() - 1, ((JComponent)this).getHeight() - 1);
  131.          }
  132.       }
  133.  
  134.       if (this.selected) {
  135.          g.setColor(this.getBorderSelectionColor());
  136.          g.drawRect(0, 0, ((JComponent)this).getWidth() - 1, ((JComponent)this).getHeight() - 1);
  137.       }
  138.  
  139.       super.paint(g);
  140.    }
  141.  
  142.    public void setBackgroundNonSelectionColor(Color newColor) {
  143.       this.backgroundNonSelectionColor = newColor;
  144.    }
  145.  
  146.    public void setBackgroundSelectionColor(Color newColor) {
  147.       this.backgroundSelectionColor = newColor;
  148.    }
  149.  
  150.    public void setBorderSelectionColor(Color newColor) {
  151.       this.borderSelectionColor = newColor;
  152.    }
  153.  
  154.    public void setClosedIcon(Icon newIcon) {
  155.       this.closedIcon = newIcon;
  156.    }
  157.  
  158.    public void setLeafIcon(Icon newIcon) {
  159.       this.leafIcon = newIcon;
  160.    }
  161.  
  162.    public void setOpenIcon(Icon newIcon) {
  163.       this.openIcon = newIcon;
  164.    }
  165.  
  166.    public void setTextNonSelectionColor(Color newColor) {
  167.       this.textNonSelectionColor = newColor;
  168.    }
  169.  
  170.    public void setTextSelectionColor(Color newColor) {
  171.       this.textSelectionColor = newColor;
  172.    }
  173. }
  174.